Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method join #2651

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Method join #2651

wants to merge 3 commits into from

Conversation

Kacper-Leman
Copy link

Soultion to tests

Copy link

@Radoslaw-Czerniawski Radoslaw-Czerniawski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs some refactoring

@@ -5,7 +5,34 @@
*/
function applyCustomJoin() {
[].__proto__.join2 = function(separator) {
// write code here
let separatorValue = separator;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use the separator parameter and get rid of this declaration along with if, if you add a default value in the parameter declaration

[].__proto__.join2 = function(separator = ',') {

separatorValue = ',';
}

const arr = this;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say arr variable is unnecessary, from the context of this implementation it's implied this refers to an array

return '';
}

for (const element of arr) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need an index regular for loop with let i = 0; i < this.length; i++ would be better


for (const element of arr) {
const str = element === null
|| element === undefined ? '' : element.toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

element.toString(); -> regarding this part, I personally am not sure whether all data types have .toString method, it would be safer to user something like String(element) or interpolate ${element}

result += this[i] === null || this[i] === undefined ? '' : String(this[i])


result += str;

if (arr.indexOf(element) < arr.length - 1) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you go with the previously suggested changes this here could be shortened to:

if (i < this.length - 1) {
  result += String(separator)
}

@Kacper-Leman
Copy link
Author

I refactor this code like you said and yea is shorter and more readable :D

Copy link

@DorotaLeniecDev DorotaLeniecDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job one suggestion from my side, for more readability, you can apply it
Approving 💪

Comment on lines 11 to 12
result += this[i] === null || this[i] === undefined
? '' : String(this[i]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion this would be even more readable:

      if (this[i] !== undefined && this[i] !== null) {
        result += String(this[i]);
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants